package homework.chapter11;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Work02 {
    public static void main(String[] args) {
//        （2）创建Collection集合students（暂时new ArrayList())
        Collection<Student> students = new ArrayList<>();
//        （3）添加本组学员Student对象到students集合中
        students.add(new Student(0001,"蒋敦豪",29));
        students.add(new Student(2,"鹭卓",29));
        students.add(new Student(3,"李耕耘",28));
        students.add(new Student(4,"李昊",27));
        students.add(new Student(5,"赵一博",27));
        students.add(new Student(6,"卓沅",26));
        students.add(new Student(7,"赵小童",25));
        students.add(new Student(8,"何浩楠",24));
        students.add(new Student(9,"陈少熙",22));
        students.add(new Student(10,"王一珩",20));

//        （4）使用foreach遍历
        System.out.println("十个勤天的学生有：");
        for (Object student : students) {
            System.out.print(" "+ student +" ");
        }
        System.out.println();

//        （5）使用Collection的remove方法删除自己，思考这样是否可以删除，如果不能，怎么办
//        students.remove(new Student(1,"蒋敦豪",29));

//        （6）使用Collection的removeIf方法删除和自己年龄一样的组员
        students.removeIf(student -> student.getAge()==20);

//        （7）使用Iterator遍历，并根据组长姓名删除组长
        String leaderName = "蒋敦豪";
        Iterator<Student> iterator = students.iterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            if (leaderName.equals(student.getName())){
                iterator.remove();
            }
        }
//        （8）最后再次使用foreach遍历students集合
        System.out.println("十个勤天的学生有：");
        for (Object student : students) {
            System.out.print(" "+ student +" ");
        }
        System.out.println();

    }
}
